package com.itheima.xmy.Demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TicketSell03 {
    private int ticket = 0;


//2.    public void run() {
//        synchronized (this) {
//            if (ticket < 1000) {
//                System.out.println(Thread.currentThread().getName() + "正在售卖" + (ticket + 1) + "张票");
//                ticket++;
//            } else {
//                System.exit(0);
//            }
//        }
//    }


    //    @Override
//   3. public  void run() {
//        while (true) {
//            run2();
//        }
//    }
//    public synchronized void  run2 (){
//        if (ticket < 1000) {
//            System.out.println(Thread.currentThread().getName() + "正在售卖" + (ticket + 1) + "张票");
//            ticket++;
//        }
//    }
    Lock lock = new ReentrantLock();

    public void run() {
        lock.lock();
        if (ticket < 1000) {
            ticket++;
            System.out.println(Thread.currentThread().getName() + "已经售卖" + ticket + "张票");
        } else {
            System.exit(0);
        }
        lock.unlock();
    }
}


